﻿using EasyNetQ.Consumer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace EasyNetQErrorHandler
{
    /// <summary>
    /// EasyNetQ消息异常处理：不回复Ack，这样MQ会把消息重新发给消费者处理
    /// </summary>
    public sealed class AlwaysRequeueErrorStrategy : IConsumerErrorStrategy
    {
        public void Dispose()
        {
        }

        public AckStrategy HandleConsumerError(ConsumerExecutionContext context, Exception exception)
        {
            //处理异常
            Console.WriteLine("Process Exception, Delay Retry!");
            //延时3秒再处理，实现了延时重试的效果
            Thread.Sleep(3 * 1000);

            //不回复Ack
            return AckStrategies.NackWithRequeue;
        }

        public AckStrategy HandleConsumerCancelled(ConsumerExecutionContext context)
        {
            Console.WriteLine("Delay Retry!");
            //延时3秒再处理，实现了延时重试的效果
            Thread.Sleep(3 * 1000);

            //不回复Ack
            return AckStrategies.NackWithRequeue;
        }
    }
}
